﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Habi.Solver.Helpers
{
    public class PathHelper
    {
        public static string Get(string path)
        {
            string filePath = RegexHelper.MatchRegUninstallPath(path);
            if (string.IsNullOrEmpty(filePath)) return string.Empty;
            if (filePath.LastIndexOf('\\') > -1) return filePath.Substring(0, filePath.LastIndexOf('\\'));

            return string.Empty;
        }

        public static string GetExtension(string filename)
        {
            return Path.GetExtension(filename);
        }

        public static string GetNameFromUrl(string url)
        {
            if (string.IsNullOrEmpty(url)) return null;

            if (url.ToLower().Contains("http"))
            {

                int index = url.LastIndexOf('/');

                if (index == -1 || index == url.Length - 1) return null;

                string name = url.Substring(index);

                if (name.LastIndexOf('.') == -1) return null;

                return name.Trim('/');
            }
            else
            {
                return Path.GetFileName(url);
            }
        }
    }
}
